package com.carbeauty.web.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carbeauty.image.indexing.ImageIndexThread;


public class ImageIndexCreateListener implements ServletContextListener {
	protected final Log log = LogFactory.getLog(getClass());
    private static ServletContext context = null;
    public static final String IMAGE_INDEX_THREAD_KEY = "ImageIndexThread";
    public static ServletContext getServletContext() {
        return context;
    }
    /**
     * Stores the reference to the {@link ServletContext}.
     * 
     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent event) {
        context = event.getServletContext();
        System.out.println("创建图片索引开始.......");
        log.info("创建图片索引开始.......");
        ImageIndexThread imageIndexThread =   new ImageIndexThread();
        imageIndexThread.createIndex();
        context.setAttribute(IMAGE_INDEX_THREAD_KEY, imageIndexThread);
    	log.info("创建图片索引结束.......");
    	System.out.println("创建图片索引结束.......");
    }

    public static ImageIndexThread getImageIndexThread(){
    	return (ImageIndexThread)context.getAttribute(IMAGE_INDEX_THREAD_KEY);
    }
    /**
     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent event) {
    	context.setAttribute(IMAGE_INDEX_THREAD_KEY, null);
    }
    
 

}
